package com.guogee.action;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.ws.rs.core.MultivaluedMap;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.guogee.bean.Community;
import com.guogee.manager.CommunityManager;
import com.guogee.util.BuildHelper;
import com.guogee.util.EncryptMD5;
import com.guogee.util.Log;
import com.sun.jersey.api.client.Client;
import com.sun.jersey.api.client.WebResource;
import com.sun.jersey.core.util.MultivaluedMapImpl;

public class DownloadDataAction {
	private Client locationClient;
	public DownloadDataAction(){
		locationClient = new Client();
	}
	
	public void getBuilding() throws Exception{
		System.out.println("获取小区信息");
		List<String> provinceList = getAllProvince();
		WebResource resource = locationClient.resource("http://icetest.colourlife.net:8081/v1/community/area");
		for(String province:provinceList){
			MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
			queryParams.add("province", province);
			String ts = ""+new Date().getTime()/1000;
			System.out.println(ts);
			String sign = BuildHelper.getAppId()+ts+BuildHelper.getToken()+"false";
			String md5Sign =  EncryptMD5.enCrypt(sign);
			System.out.println(md5Sign);
			queryParams.add("sign",md5Sign);
			
			queryParams.add("ts", ts);
			queryParams.add("appID", BuildHelper.getAppId());
			String result = resource.queryParams(queryParams).get(String.class);
			JSONObject json = (JSONObject) JSONObject.parse(result);
			int code = json.getIntValue("code");
			if(code == 0){
				//获取成功
				JSONArray communityList = json.getJSONArray("content");
				if(communityList != null && communityList.size() > 0){
					parseContent(communityList);
				}
			}
			//System.out.println(json);
		}
		
	
	}
	
	public void sendSMS() throws Exception{
		WebResource resource = locationClient.resource("http://test.guogee.com:8087/message/sendSMS");
		MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
	}
	
	public String getAccessToken() throws Exception{
		System.out.println("获取token");
	//	http://icetest.colourlife.net:8081/v1/jqfw/app/auth?ts=1505739921&sign=1189fd2d7b9b0c3cce7865669d46267e&appID=ICECZJIA-27CD-4432-A110-45B64B335C70
		WebResource resource = locationClient.resource("http://icetest.colourlife.net:8081/v1/jqfw/app/auth");
		MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
		String ts = ""+new Date().getTime()/1000;
		ts = "1505911738";
		System.out.println(ts);
		String signature = BuildHelper.getAppKey()+ts+BuildHelper.getAppSecret();
		String sign = BuildHelper.getAppId()+ts+BuildHelper.getToken()+"false";
		String md5Signature =  EncryptMD5.enCrypt(signature);
		String md5Sign =  EncryptMD5.enCrypt(sign);
		System.out.println("signature:"+md5Signature);
		System.out.println("sign:"+md5Sign);
	
		queryParams.add("appID", BuildHelper.getAppId());
		queryParams.add("appkey", BuildHelper.getAppKey());
		queryParams.add("timestamp", ts);
		queryParams.add("ts", ts);
		queryParams.add("signature",md5Signature);
		queryParams.add("sign",md5Sign);
		String result = resource.queryParams(queryParams).post(String.class);
		System.out.println(result);
		JSONObject json = (JSONObject) JSONObject.parse(result);
		int code = json.getIntValue("code");
		if(code == 0){
			//获取成功
			JSONObject content = json.getJSONObject("content");
			String accessToken = content.getString("access_token");
			return accessToken;
		
		}
		return null;
	}
	
	public String getAccessToken2() throws Exception{
		System.out.println("获取小区信息");
		WebResource resource = locationClient.resource("http://icetest.colourlife.net:8081/v1/jqfw/app/auth");
		MultivaluedMap<String, String> queryParams = new MultivaluedMapImpl();
		String ts = ""+new Date().getTime()/1000;
		ts = "1505889968";
		System.out.println(ts);
		//String signature = BuildHelper.getAppKey()+ts+BuildHelper.getAppSecret();
		String signature = BuildHelper.getAppUuid()+ts+BuildHelper.getAppSecretAuth2();
		String sign = BuildHelper.getAppId()+ts+BuildHelper.getToken()+"false";
		String md5Signature =  EncryptMD5.enCrypt(signature);
		String md5Sign =  EncryptMD5.enCrypt(sign);
		System.out.println("signature:"+md5Signature);
		System.out.println("sign:"+md5Sign);
	
		queryParams.add("appID", BuildHelper.getAppId());
		queryParams.add("appkey", BuildHelper.getAppUuid());
		queryParams.add("timestamp", ts);
		queryParams.add("ts", ts);
		queryParams.add("signature",md5Signature);
		queryParams.add("sign",md5Sign);
		String result = resource.queryParams(queryParams).post(String.class);
		System.out.println(result);
		JSONObject json = (JSONObject) JSONObject.parse(result);
		int code = json.getIntValue("code");
		if(code == 0){
			//获取成功
			JSONObject content = json.getJSONObject("content");
			String accessToken = content.getString("access_token");
			return accessToken;
		
		}
		return null;
	}

	
	private void parseContent(JSONArray communityObjList){
		List<Community> dbObjList = new ArrayList<Community>();
		for(Object obj:communityObjList){
			JSONObject jObj = (JSONObject) obj;
			Community c = new Community();
			c.setUuid(jObj.getString("uuid"));
			c.setName(jObj.getString("name"));
			c.setCountry(jObj.getString("country"));
			c.setProvince(jObj.getString("province"));
			c.setCity(jObj.getString("city"));
			c.setRegion(jObj.getString("region"));
			Log.i(c.toString());
			dbObjList.add(c);
		}
		CommunityManager.getInstance().saveCommunity(dbObjList);
	}
	private List<String> getAllProvince(){
		List<String> provinceList = new ArrayList<String>();
		provinceList.add("河北");
		provinceList.add("山西");
		provinceList.add("辽宁");
		provinceList.add("吉林");
		provinceList.add("黑龙江");
		provinceList.add("江苏");
		provinceList.add("浙江");
		provinceList.add("安徽");
		provinceList.add("福建");
		provinceList.add("江西");
		provinceList.add("山东");
		provinceList.add("河南");
		provinceList.add("湖北");
		provinceList.add("湖南");
		provinceList.add("广东");
		provinceList.add("海南");
		provinceList.add("四川");
		provinceList.add("贵州");
		provinceList.add("云南");
		provinceList.add("陕西");
		provinceList.add("甘肃");
		provinceList.add("青海");
		provinceList.add("台湾");
		provinceList.add("北京");
		provinceList.add("上海");
		provinceList.add("重庆");
		provinceList.add("天津");
		provinceList.add("广西壮族自治区");
		provinceList.add("宁夏回族自治区");
		provinceList.add("西藏自治区");
		provinceList.add("新疆维吾尔自治区");
		provinceList.add("内蒙古自治区");
		provinceList.add("香港");
		provinceList.add("澳门");
		return provinceList;
	}
	public static void main(String[] args) {
		DownloadDataAction action = new DownloadDataAction();
		try {
			System.out.println((0x80 &0x80) >> 7);
			
			//action.getBuilding();
			action.getAccessToken();
		//	action.getAccessToken2();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
